﻿using System;
using System.Windows.Forms;
using SampleMessages;
using SimpleServiceBus.Configuration;
using SimpleServiceBus.Endpoint;
using SimpleServiceBus.EndpointManagement.Server.Configuration;
using SimpleServiceBus.Endpoints.Msmq;
using SimpleServiceBus.Endpoints.Msmq.Configuration;

namespace SampleMonitorService
{
    internal delegate void NotifyDelegate(string message);

    public partial class MainForm : Form
    {
        private IMessagingEndpoint _endpoint;

        public MainForm()
        {
            InitializeComponent();
        }

        public static MainForm Current { get; set; }
        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Current = this;
            btnStartStop_Click(null, null);
        }

        private void btnStartStop_Click(object sender, EventArgs e)
        {
            try
            {
                if (_endpoint == null)
                    CreateEndpoint();

                if (_endpoint != null)
                    if (_endpoint.IsRunning)
                        StopEndpoint();
                    else
                        StartEndpoint();
            }
            catch (Exception ex)
            {
                Notify(ex.Message + Environment.NewLine + ex.StackTrace);
                while (ex.InnerException != null)
                {
                    Notify(ex.InnerException.Message + Environment.NewLine + ex.StackTrace);
                    ex = ex.InnerException;
                }
            }
        }

        private void CreateEndpoint()
        {
            MsmqHelper.EnsureQueue("EndpointMonitorService");

           

          //Create the endpoint
            IEndpointBuilder builder = MsmqEndpointConfiguration.ConfigureEndpoint();
            EndpointMonitorConfiguration.ConfigureEndpointAsEndpointMonitor(builder);
            builder.MessageHandlers.RegisterMessageHandlerAssembly(GetType().Assembly);
            _endpoint = builder.Endpoint;
        }


        private void StartEndpoint()
        {
            Notify("Starting Endpoint");

            _endpoint.Start();

            //Subscribe to test publication
            _endpoint.MessageBus.Subscribe<TestPublicationMessage>();

            Notify("Endpoint Started");
            btnStartStop.Text = "Stop";
        }

        private void StopEndpoint()
        {
            Notify("Stopping Endpoint");

            _endpoint.Stop();

            Notify("Endpoint Stopped");
            btnStartStop.Text = "Start";
        }

        public void Notify(string message)
        {
            if (InvokeRequired)
            {
                NotifyDelegate marshal = Notify;
                BeginInvoke(marshal, message);
            }
            else
            {
                txtStatus.Text += message + Environment.NewLine;
                txtStatus.Refresh();
            }
        }

       
    }
}